package com.cloudkinto.service.orderwork.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.OrderCodeUtil;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.utils.ali.AliOss;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.company.CompanyService;
import com.cloudkinto.service.dic.DicBusinessItemService;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.orderback.OrderBackService;
import com.cloudkinto.service.orderwork.OrderWorkService;
import com.cloudkinto.service.orderwork.vo.*;
import com.cloudkinto.service.shop.ShopService;
import com.cloudkinto.service.stock.vo.log.StockLogExportVo;
import com.cloudkinto.service.storage.StorageService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-06-18
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class OrderWorkServiceImpl extends ServiceImpl<OrderWorkDao, OrderWorkDo> implements OrderWorkService {
    @Autowired
    private OrderWorkDao dao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private StorageService storageService;
    @Autowired
    private DicBusinessItemDao dicBusinessItemDao;
    @Autowired
    private DicBusinessItemService dicBusinessItemService;
    @Autowired
    private OrderWorkPriceDao orderWorkPriceDao;
    @Autowired
    private OrderWorkProductDao orderWorkProductDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private ShopService shopService;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CostRecordService costRecordService;
    @Autowired
    private ExchangeRateDao exchangeRateDao;
    @Value("${gbc.uploadPath.fileUrl}")
    private String fileUrl;

    @Value("${gbc.uploadPath.filePathTemp}")
    private String filePathTemp;
    @Autowired
    private OrderBackService ordrBackServicve;

    @Override
    public SingleResult addInit(Long userId, Long companyId) {
        Map result = new HashMap();
        result.put("storageList", storageService.getStorageList());
//        result.put("shopList", shopService.selectShopListInit(userId, companyId));

        result.put("orderWorkTypeList", dicBusinessItemService.getDicItemList(DicBusinessConstant.orderWorkType));
//        List<Map> companyList = new ArrayList<>();
//        List<CompanyDo> companyDoList = companyDao.selectList(null);
//        if (!CollectionUtils.isEmpty(companyDoList)) {
//            companyDoList.forEach(companyDo -> {
//                Map map = new HashMap();
//                map.put("id", companyDo.getId());
//                map.put("value", companyDo.getCompanyCode());
//                companyList.add(map);
//            });
//        }
//
//        result.put("companyList", companyList);
        result.put("companyTree", companyService.tree());
        return SingleResult.success(result);
    }

    @Override
    public OrderWorkDo add(OrderWorkAddReq req, Long userId) {
        OrderWorkDo entityDo = new OrderWorkDo();
        entityDo.setStatus(0);
        entityDo.setWorkType(req.getWorkType());
        entityDo.setShopId(req.getShopId());
        entityDo.setCompanyId(req.getCompanyId());
        entityDo.setStorageId(req.getStorageId());
        entityDo.setContent(req.getContent());
        entityDo.setSysTips(req.getSysTips());
        StringBuilder stringBuilder = new StringBuilder();
        if (req.getFileId() != null) {
            for (String id : req.getFileId()) {
                if (stringBuilder.length() < 1) {
                    stringBuilder.append(id);
                } else {
                    stringBuilder.append("," + id);
                }
            }
            entityDo.setFileId(stringBuilder.toString());
            fileService.uploadFileMult(req.getFileId());
        }
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        entityDo.setWorkNumber(OrderCodeUtil.getWorkOrderNumber(entityDo.getId()));
        dao.updateById(entityDo);

        dealwithPriceAndProduct(req, entityDo);
        return entityDo;
    }

    @Override
    public void setFee(OrderWorkSetFeeReq req, Long userId) {
        OrderWorkDo orderWorkDo = dao.selectById(req.getId());
        if (orderWorkDo == null) {
            throw new BizException(SysConstant.Error_Data_NotExist);
        }
        orderWorkDo.setUpdateBy(userId);
        orderWorkDo.setUpdateTime(new Date());
        dao.updateById(orderWorkDo);

        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, orderWorkDo.getId());
        orderWorkPriceDao.delete(wrapper);

        for (OrderWorkSetFeeDetail feeDetail : req.getFeeDetailList()) {
            if (StringUtils.isBlank(feeDetail.getName())) {
                throw new BizException(SysConstant.Pls_Fill_FeeName);
            } else if (feeDetail.getPrice() == null) {
                throw new BizException(SysConstant.Pls_Fill_FeePrice);
            } else if (feeDetail.getCurrency() == null) {
                throw new BizException(SysConstant.Pls_Fill_CurrencyType);
            }

            OrderWorkPriceDo orderWorkPriceDo = new OrderWorkPriceDo();
            orderWorkPriceDo.setPrice(feeDetail.getPrice());
            orderWorkPriceDo.setName(feeDetail.getName());
            orderWorkPriceDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
            orderWorkPriceDo.setOrderWorkId(orderWorkDo.getId());
            orderWorkPriceDo.setCurrency(feeDetail.getCurrency());
            orderWorkPriceDao.insert(orderWorkPriceDo);
        }

    }

    @Override
    public SingleResult setFeeInit(Long id) {
        List<Map> result = new ArrayList<>();

        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, id);
        List<OrderWorkPriceDo> orderWorkPriceDos = orderWorkPriceDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(orderWorkPriceDos)) {
            for (OrderWorkPriceDo orderWorkPriceDo : orderWorkPriceDos) {
                Map map = new HashMap();
                map.put("name", orderWorkPriceDo.getName());
                map.put("price", orderWorkPriceDo.getPrice());
                map.put("currency", orderWorkPriceDo.getCurrency());
                result.add(map);
            }
        }
        return SingleResult.success(result);
    }

    private void dealwithPriceAndProduct(OrderWorkAddReq req, OrderWorkDo entityDo) {
        if (!CollectionUtils.isEmpty(req.getPriceAddList())) {
            req.getPriceAddList().forEach(orderWorkPriceAdd -> {
                if (orderWorkPriceAdd != null && orderWorkPriceAdd.getName() != null && orderWorkPriceAdd.getPrice() != null) {
                    OrderWorkPriceDo orderWorkPriceDo = new OrderWorkPriceDo();
                    orderWorkPriceDo.setOrderWorkId(entityDo.getId());
                    orderWorkPriceDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
                    orderWorkPriceDo.setName(orderWorkPriceAdd.getName());
                    orderWorkPriceDo.setPrice(orderWorkPriceAdd.getPrice());
                    orderWorkPriceDao.insert(orderWorkPriceDo);
                }
            });
        }
        if (req.getProductAddList()==null)return;
        req.getProductAddList().forEach(orderWorkProductAdd -> {
            if (orderWorkProductAdd != null && orderWorkProductAdd.getProductInfoId() != null & orderWorkProductAdd.getCount() != null) {
                OrderWorkProductDo orderWorkProductDo = new OrderWorkProductDo();
                orderWorkProductDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
                orderWorkProductDo.setCount(orderWorkProductAdd.getCount());
                orderWorkProductDo.setProductInfoId(orderWorkProductAdd.getProductInfoId());
                orderWorkProductDo.setOrderWorkId(entityDo.getId());
                orderWorkProductDao.insert(orderWorkProductDo);
            }
        });
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(OrderWorkDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }


    @Override
    public SingleResult updateInit(Long id, Long currentUserId, Long companyId) {
        Map result = (Map) addInit(currentUserId, companyId).getData();
        result.put("detail", detail(id));

        return SingleResult.success(result);
    }

    @Override
    public OrderWorkUpdateReq update(OrderWorkUpdateReq req, Long userId) {
        OrderWorkDo entityDo = this.baseMapper.selectById(req.getId());
        if (req.getFileId() != null) {
//            fileService.deleteFile(entityDo.getFileId());
            fileService.uploadFileMult(req.getFileId());
            StringBuilder stringBuilder = new StringBuilder();
            for (String id :
                    req.getFileId()) {
                if (stringBuilder.length() < 1) {
                    stringBuilder.append(id);
                } else {
                    stringBuilder.append("," + id);
                }
            }
            entityDo.setFileId(stringBuilder.toString());
        }
        entityDo.setWorkType(req.getWorkType());
        entityDo.setShopId(req.getShopId());
        entityDo.setStorageId(req.getStorageId());
        entityDo.setCompanyId(req.getCompanyId());

        entityDo.setContent(req.getContent());
        entityDo.setSysTips(req.getSysTips());
        handlerAddBefore(entityDo, userId);
        dao.updateById(entityDo);

//        LambdaQueryWrapper<OrderWorkPriceDo> priceWrapper = new LambdaQueryWrapper<>();
//        priceWrapper.eq(OrderWorkPriceDo::getOrderWorkId, req.getId());
//        orderWorkPriceDao.delete(priceWrapper);

        LambdaQueryWrapper<OrderWorkProductDo> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(OrderWorkProductDo::getOrderWorkId, req.getId());
        orderWorkProductDao.delete(productWrapper);

        dealwithPriceAndProduct(req, entityDo);

        return req;
    }

    @Override
    public SingleResult updateStatus(Long id, String status, String sysTips, Long currentUserId, Long companyId) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        wrapper.set("sys_tips", sysTips);
        wrapper.set("update_by", currentUserId);
        wrapper.set("update_time", new Date());
        ExchangeRateDo exchangeRateDo = exchangeRateDao.getJPRate();
        OrderWorkDo workDo = baseMapper.selectById(id);
        boolean b = update(wrapper);
        if (b && workDo.getStatus() != 3) {
            QueryWrapper wrapper1 = new QueryWrapper();

            wrapper1.eq("order_work_id", id);
            wrapper1.isNotNull("name");
            wrapper1.isNotNull("price");
            List<OrderWorkPriceDo> priceDoList = orderWorkPriceDao.selectList(wrapper1);
            if (priceDoList != null && priceDoList.size() > 0) {
                double totalFeeJpy = 0D;
                boolean jpy = true;
                for (OrderWorkPriceDo priceDo : priceDoList) {
                    if (priceDo.getCurrency() == null || priceDo.getCurrency() == 1L) { //日币
                        jpy = true;
                        totalFeeJpy += priceDo.getPrice();
                    } else if (priceDo.getCurrency() == 0L) {//人民币
                        totalFeeJpy += priceDo.getPrice() / exchangeRateDo.getRate();
                    }
                }
                if ("3".equals(status)) {
                    if (jpy) {
                        costRecordService.add(3, workDo.getWorkNumber() + workDo.getContent(), totalFeeJpy,
                                workDo.getShopId(), workDo.getCompanyId(), id);
                    }
//                    if (rmb) {
//                        costRecordService.addRmb(3, workDo.getWorkNumber() + workDo.getContent(), totalFeeRmb,
//                                workDo.getShopId(), workDo.getCompanyId(), id);
//                    }
                }
            }
        }
        return b ? SingleResult.success() : SingleResult.failure("更新失败");
    }

    @Override
    public SingleResult updateStatusInit(Long currentUserId, Long companyId, Long id) {
        List<List> all = new ArrayList<>();
        OrderWorkDo orderWorkDo = dao.selectById(id);
        List<Map> orderWork = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("status", orderWorkDo.getStatus());
        map.put("sysTips", orderWorkDo.getSysTips());
        orderWork.add(map);
        List<Map> statusList = getStatusList();
        all.add(orderWork);
        all.add(statusList);
        return SingleResult.success(all);
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(OrderWorkDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }


    @Override
    public int delete(Long id, Long userId) {
        OrderWorkDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        dao.updateById(entityDo);
        dao.deleteById(id);

        LambdaQueryWrapper<OrderWorkPriceDo> priceWrapper = new LambdaQueryWrapper<>();
        priceWrapper.eq(OrderWorkPriceDo::getOrderWorkId, id);
        orderWorkPriceDao.delete(priceWrapper);

        LambdaQueryWrapper<OrderWorkProductDo> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(OrderWorkProductDo::getOrderWorkId, id);
        orderWorkProductDao.delete(productWrapper);
        if (StringUtils.isNotBlank(entityDo.getFileId()))
            fileService.deleteFileMulti(entityDo.getFileId());
        return 1;
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(OrderWorkDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public OrderWordDetailRes detail(Long id) {
        OrderWorkDo entityDo = dao.selectById(id);
        OrderWordDetailRes res = new OrderWordDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        CompanyDo companyDo = companyDao.selectById(entityDo.getCompanyId());
        if (companyDo != null) {
            res.setCompanyCode(companyDo.getCompanyCode());
            res.setCompanyName(companyDo.getName());
        }
        ShopDo shopDo = shopDao.selectById(entityDo.getShopId());
        if (shopDo != null) {
            res.setShopName(shopDo.getShopName());
        }

        return handlerDetailAfter(res, entityDo);
    }

    //查询明细之后要执行的业务定制处理
    private OrderWordDetailRes handlerDetailAfter(OrderWordDetailRes res, OrderWorkDo orderWorkDo) {
        ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
        if (shopDo != null) {
            res.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
        if (storageDo != null) {
            res.setStorageName(storageDo.getStorageName());
        }
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
        if (businessItemDo != null) {
            res.setWorkTypeName(businessItemDo.getDicItemValue());
        }
        res.setStatusName(getStatusStr(orderWorkDo.getStatus()));

        res.setContent(orderWorkDo.getContent());
        res.setSysTips(orderWorkDo.getSysTips());
        res.setFileUrl(fileService.getFilePathMulti(orderWorkDo.getFileId()));

        List<OrderWorkPriceDetail> priceDetailList = new ArrayList<>();
        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, orderWorkDo.getId());
        List<OrderWorkPriceDo> orderWorkPriceDoList = orderWorkPriceDao.selectList(wrapper);

        Double totalPrice = 0D;
        if (!CollectionUtils.isEmpty(orderWorkPriceDoList)) {
            for (OrderWorkPriceDo orderWorkPriceDo : orderWorkPriceDoList) {
                OrderWorkPriceDetail priceDetail = new OrderWorkPriceDetail();
                priceDetail.setName(orderWorkPriceDo.getName());
                priceDetail.setPrice(orderWorkPriceDo.getPrice());
                priceDetail.setCurrency(orderWorkPriceDo.getCurrency());
                if (orderWorkPriceDo.getPrice() != null) {
                    totalPrice += orderWorkPriceDo.getPrice();
                }
                priceDetailList.add(priceDetail);
            }
        }
        res.setTotalPrice(totalPrice);
        res.setPriceDetailList(priceDetailList);

        List<OrderWorkProductDetail> productDetailList = new ArrayList<>();
        LambdaQueryWrapper<OrderWorkProductDo> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(OrderWorkProductDo::getOrderWorkId, orderWorkDo.getId());
        List<OrderWorkProductDo> workProductDoList = orderWorkProductDao.selectList(productWrapper);
        if (!CollectionUtils.isEmpty(workProductDoList)) {
            workProductDoList.forEach(orderWorkProductDo -> {
                OrderWorkProductDetail productDetail = new OrderWorkProductDetail();
                productDetail.setCount(orderWorkProductDo.getCount());
                productDetail.setProductInfoId(orderWorkProductDo.getProductInfoId());
                ProductInfoDo productInfoDo = productInfoDao.selectById(orderWorkProductDo.getProductInfoId());
                if (productInfoDo != null) {
                    productDetail.setProductName(productInfoDo.getProductName());
                    productDetail.setCommoditySku(productInfoDo.getCommoditySku());
                    DicBusinessItemDo businessItemDo2 = dicBusinessItemDao.selectById(productInfoDo.getProductType());
                    if (businessItemDo2 != null) {
                        productDetail.setProductType(businessItemDo2.getDicItemValue());
                    }
                    productDetail.setStorageSku(productInfoDo.getStorageSku());
                }
                productDetailList.add(productDetail);
            });
        }
        res.setProductDetailList(productDetailList);
        return res;
    }

    @Override
    public SingleResult pageInit() {
        Map result = new HashMap();
        result.put("orderWorkTypeList", dicBusinessItemService.getDicItemList(DicBusinessConstant.orderWorkType));

        List<Map> currencyTypeList = new ArrayList<>();
        Map currencyType = new HashMap();
        currencyType.put("id", 0);
        currencyType.put("value", "人民币");
        currencyTypeList.add(currencyType);
        currencyType = new HashMap();
        currencyType.put("id", 1);
        currencyType.put("value", "日币");
        currencyTypeList.add(currencyType);

        result.put("currencyTypeList", currencyTypeList);


        result.put("statusList", getStatusList());
        result.put("storageList", storageService.getStorageList());
        return SingleResult.success(result);
    }

    private List<Map> getStatusList() {
        List<Map> statusList = new ArrayList<>();
        Map map = new HashMap();
        map.put("id", 0);
        map.put("value", StringsUtil.createI18Message("未审核"));
        statusList.add(map);

        map = new HashMap();
        map.put("id", 1);
        map.put("value", StringsUtil.createI18Message("已审核"));
        statusList.add(map);

        map = new HashMap();
        map.put("id", 2);
        map.put("value", StringsUtil.createI18Message("已处理"));
        statusList.add(map);

        map = new HashMap();
        map.put("id", 3);
        map.put("value", StringsUtil.createI18Message("已完成"));
        statusList.add(map);
        return statusList;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<OrderWorkDo> wrapper = queryBuild(map);
        IPage<OrderWorkDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<OrderWorkPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<OrderWorkPageRes> handlerListAfter(List<OrderWorkDo> list) {
        List<OrderWorkPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(orderWorkDo -> {
                OrderWorkPageRes res = new OrderWorkPageRes();
                res.setId(orderWorkDo.getId());
                res.setContent(orderWorkDo.getContent());
                res.setSysTips(orderWorkDo.getSysTips());
                res.setWorkNumber(orderWorkDo.getWorkNumber());
                ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
                if (shopDo != null) {
                    res.setShop(shopDo.getShopName());
                }
                StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
                if (storageDo != null) {
                    res.setStorage(storageDo.getStorageName());
                }
                DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
                if (businessItemDo != null) {
                    res.setWorkType(businessItemDo.getDicItemValue());
                }
                CompanyDo companyDo = companyDao.selectById(orderWorkDo.getCompanyId());
                if (companyDo != null) {
                    res.setCompanyCode(companyDo.getCompanyCode());
                }

                res.setStatus(getStatusStr(orderWorkDo.getStatus()));
                result.add(res);
            });
        }
        return result;
    }

    private IPage<OrderWorkDo> pageInit(Map<String, String> map) {
        IPage<OrderWorkDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<OrderWorkDo> queryBuild(Map<String, String> map) {
        QueryWrapper<OrderWorkDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.and(o -> {
                o.like("work_number", map.get("keyword").trim())
                        .or().like("content", map.get("keyword").trim());
            });
        }
        if (StringUtils.isNotBlank(map.get("status"))) {
            wrapper.eq("status", map.get("status"));
        }
        if (StringUtils.isNotBlank(map.get("orderWorkType"))) {
            wrapper.eq("work_type", map.get("orderWorkType"));
        }
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        if (StringUtils.isNotBlank(map.get("storageId"))) {
            wrapper.eq("storage_id", map.get("storageId"));
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Override
    public String exportExcel(List<Long> ids, String fileName) {
        QueryWrapper<OrderWorkDo> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        List<OrderWorkDo> list = dao.selectList(wrapper);
        if (list.size() > 9999) {
            throw new BizException(SysConstant.File_Export_ToMuch, 10000);
        }
        List<OrderWorkExportVo> exportVoList = new ArrayList<>();
        list.forEach(et -> {
            OrderWorkExportVo exportVo = new OrderWorkExportVo();
            exportVo.setOrderNo(et.getWorkNumber());
            exportVo.setContent(et.getContent());
            exportVo.setCompanyCode(companyDao.selectById(et.getCompanyId()).getCompanyCode());
            exportVo.setFeedback(et.getSysTips());
            if (et.getContent().length() > 14 && et.getContent().indexOf("BO") >= 0) {
                String ordrBackNo = et.getContent().substring(et.getContent().indexOf("BO"), et.getContent().indexOf("BO") + 14);
                if (StringUtils.isNotBlank(ordrBackNo)) {
                    QueryWrapper<OrderBackDo> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("order_out_number", ordrBackNo);
                    OrderBackDo one = ordrBackServicve.getOne(wrapper1);
                    if (one != null) {
                        exportVo.setTrackName(one.getTrackName());
                        exportVo.setTrackNumber(one.getTrackNumber());
                        exportVo.setDealTime(TimeUtils.formatDate(one.getCreateTime()));
                    }
                }
            }
            exportVoList.add(exportVo);
        });
        ExportParams exportParams = new ExportParams();
        exportParams.setCreateHeadRows(true);
        exportParams.setHeaderColor(IndexedColors.YELLOW.getIndex());
        String objectNameTemporary = "";
        try {
            ExcelUtils.exportExcelToSave(exportVoList, OrderWorkExportVo.class, fileName, exportParams, filePathTemp);
            objectNameTemporary = AliOss.temporaryFile2(fileName, new File(filePathTemp + File.separator + fileName));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出文件失败" + e.getMessage());
        }
        return fileUrl + objectNameTemporary;
    }

    private String getStatusStr(Integer type) {
        if (type == null) return "";
        else if (type == 0) return "未审核";
        else if (type == 1) return "已审核";
        else if (type == 2) return "已处理";
        else if (type == 3) return "已完成";
        else return "";
    }

}
